package com.eunion.core.common.jpa.query;

import java.util.Collections;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

public interface QueryRequirement<Entity, Bean> extends Specification<Entity> {

    Predicate where(Root<Entity> root, CriteriaQuery<Bean> query, CriteriaBuilder cb);

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    default Predicate toPredicate(Root<Entity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        return where(root, (CriteriaQuery) query, cb);
    }

    default List<Expression<?>> selections(Root<Entity> root, CriteriaQuery<Bean> query, CriteriaBuilder cb) {
        return Collections.emptyList();
    }

    default List<Expression<?>> groupBy(Root<Entity> root) {
        return Collections.emptyList();
    }

    default void applyFetches(Root<Entity> root) {
        // do nothing by default
    }

}
